<?php

namespace Repository;

use Doctrine\ORM\EntityRepository;

class CartRepository extends EntityRepository {
	public function backendPaginate($conf){
		$q = $this->_em->getRepository('\Entity\ShCart')->findBy(
			$conf['condition'],$conf['order'],$conf['limit'],$conf['offset']
			);
		return $q;
	}
	public function addCarts($data){
		$c = new \Entity\ShCart();

		$c->setCusname($data->post('ten-khach-hang'));
		$c->setCusemail($data->post('email'));
		$c->setCusphone($data->post('so-dt'));
		$c->setCusaddress($data->post('dia-chi'));
		$c->setDaterequest(time());
		$c->setDatedelivery(time());
		$c->setStatus($data->post('tinh-trang'));
		$c->setTotalmoney($data->post('tong-tien'));


		$this->_em->persist($c);
		$this->_em->flush();
	}
	
	public function editCart($data, $cart){
		$cd = $this->_em->getRepository('\Entity\ShCartdetail')->findBy(array(
			'shCart'=> $data['id']
		));

		foreach($cd as $v):
			$id = $v->getProduct()->getId();
			if(!empty($data['product'][$id])):
				$v->setQuantity($data['product'][$id]['qty']);
				$this->_em->persist($v);
			endif;
		endforeach;
		$this->_em->flush();

		$cart->setStatus($data['status']);
		$this->_em->persist($cart);
		$this->_em->flush();
	}

	public function checkOut($info, $cart, $id, $total){
		if(empty($info)) exit('asdasd');

		$u = $this->_em->getRepository('\Entity\ShUser')->findOneById($id);

		if(empty($u))
			throw new \Exception('Cant find this user');

		$new_c = new \Entity\ShCart();
		$new_c->setUser($u);
		$new_c->setCusname($info->post('ten-khach-hang'));
		$new_c->setCusphone($info->post('sdt'));
		$new_c->setCusemail($info->post('email'));
		$new_c->setCusaddress($info->post('dc'));
		$new_c->setDetail($info->post('detail'));
		$new_c->setTotalmoney($total);
		$new_c->setDaterequest(time());
		$new_c->setStatus(0);
		$this->_em->persist($new_c);
		$this->_em->flush();

		foreach($cart as $v):
			$p = $this->_em->getRepository('\Entity\ShProduct')->findOneById($v['id']);
			if(empty($p))
				continue;

			$cd = new \Entity\ShCartdetail();
			$cd->setProduct($p);
			$cd->setCart($new_c);
			$cd->setQuantity($v['qty']);
			$cd->setPrice($v['price']);

			if(!empty($v['options']['color'])):
				$colour = $this->_em->getRepository('\Entity\ShExtension')->findOneById($v['options']['color']);
				if(!empty($colour))
					$cd->setColor($colour);
			endif;

			if(!empty($v['options']['size'])):
				$size = $this->_em->getRepository('\Entity\ShExtension')->findOneById($v['options']['size']);
				if(!empty($size))
					$cd->setSize($size);
			endif;
			$this->_em->persist($cd);
		endforeach;
		$this->_em->flush();
	}

	public function countCart($id){
		$q = $this->_em->createQueryBuilder()
						->select('COUNT(c.id) as total')
						->from('\Entity\ShCart','c')
						->where('c.shUser = ?1')
						->andWhere('c.status = 0')
						->setParameters(array(1 => $id))
						->getQuery()->getSingleScalarResult();
		return $q;
	}
	
	public function total_rows($pag){
		$q = $this->_em->createQueryBuilder()
		->select('COUNT(c.id)')
		->from('Entity\ShCart','c');
		return $q->getQuery()->getSingleScalarResult();
	}
}